/**
 * All rights Reserved, Designed By senior.auto
 *
 * @Title: DataSourceConfig.java
 * @Package com.example.demo.config
 * @Description: 数据源配置
 * @author xiongzhengsheng
 * @date 2021年1月12日 下午8:22:43
 * @version V1.0
 * @Copyright: 20210 senior.auto Inc. All rights reserved.
 */
package com.example.demo.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

/**
 * @ClassName: DataSourceConfig
 * @Description: 数据源配置
 * @author: xiongzhengsheng
 * @date: 2021年1月12日 下午8:21:48
 * @Copyright: 2021 senior.auto Inc. All rights reserved.
 *
 */
@Configuration
public class DataSourceConfig {

    /**
     * @Title: sqlSessionFactory
     * @Description: 配置sqlSessionFactory工厂bean
     * @param: @throws Exception 参数
     * @return: SqlSessionFactory 返回类型
     * @throws
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.senior.cloud.system.model");

        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
		configuration.addInterceptor(paginationInterceptor());
        sqlSessionFactoryBean.setConfiguration(configuration);
        sqlSessionFactoryBean.setTypeHandlersPackage("com.senior.cloud.system.typehandler");

        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mappers/*.xml"));
        sqlSessionFactoryBean.setTypeAliasesPackage("com.senior.cloud.system.model");

        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }
    
    /**
     * @Title: paginationInterceptor
     * @Description: 分页插件
     * @param: @return 参数
     * @return: PaginationInterceptor 返回类型
     * @throws
     */
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}